package com.SmartCare.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.SmartCare.common.enums.Constants;
import com.SmartCare.common.Result;
import com.SmartCare.common.dto.BillDTO;
import com.SmartCare.common.exception.ServiceException;
import com.SmartCare.service.BillService;
import com.SmartCare.mapper.role.ElderlyInformationMapper;
import com.SmartCare.pojo.Bill;
import com.SmartCare.mapper.BillMapper;
import com.SmartCare.pojo.role.ElderlyInformation;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 账单信息 服务实现类
 */
@Service
public class BillServiceImpl extends ServiceImpl<BillMapper, Bill> implements BillService {


    @Override
    public Result getAll() {
        return Result.success(list());
    }

    @Override
    public Result getBillById(Long id) {
        QueryWrapper<Bill> qw = new QueryWrapper<>();
        qw.eq("user_id", id);
        List<Bill> list = list(qw);
        return Result.success(list);
    }

    @Override
    public Result add(BillDTO billDto) {
        //判断其他属性是否传入
        if (billDto.getAmount() == null|| billDto.getAmount()<=0) {
            throw new ServiceException(Constants.CODE_400, "请输入合法的消费金额");
        }
        Bill bill = new Bill();
        BeanUtils.copyProperties(billDto,bill);
        //保存
        save(bill);
        return Result.success("添加记录成功");
    }

    @Override
    public Result delete(List<Long> ids) {
        boolean res = removeByIds(ids);
        if (res){
            return Result.success("删除成功");
        }
        return Result.error("删除失败");
    }
}
